package com.prs.crm.action.oa;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.MailItem;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.oa.MailItemService;

public class MailItemAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MailItemService mailItemService;

	private MailItem item;

	private PaginationSupport paginiation;

	private Integer[] itemIds;

	private File[] upload;

	private String[] uploadFileName;

	public void setMailItemService(MailItemService mailItemService) {
		this.mailItemService = mailItemService;
	}

	public MailItemService getMailItemService() {
		return mailItemService;
	}

	public void setItem(MailItem item) {
		this.item = item;
	}

	public MailItem getItem() {
		return item;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setPaginiation(PaginationSupport paginiation) {
		this.paginiation = paginiation;
	}

	public PaginationSupport getPaginiation() {
		return paginiation;
	}

	public void setItemIds(Integer[] itemIds) {
		this.itemIds = itemIds;
	}

	public Integer[] getItemIds() {
		return itemIds;
	}

	@Action("listMailInBox")
	public String execute() {
		this.setPaginiation(this.getMailItemService().listInBox(
				this.getPaginationHelper().createQuery(MailItem.class),
				this.getPaginationHelper().prepare(paginiation),
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("viewReciveMail")
	public String viewReciveMail() {
		this.getMailItemService().executeRead(item);
		return SUCCESS;
	}

	@Action(value = "removeReciveMail", results = { @Result(name = "success", type = "redirect", location = "listMailInBox") })
	public String removeReciveMail() {
		this.getMailItemService().batchRemove(itemIds);
		return SUCCESS;
	}

	/**
	 * 撰写邮件
	 */
	@Action("createMail")
	public String createMail() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "sendMail", results = {
			@Result(name = "success", type = "redirect", location = "createMail"),
			@Result(name = "error", type = "chain", location = "createMail") })
	public String sendMail() {
		if (item.getTitle() == null || "".equals(item.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (item.getSendTo() == null || "".equals(item.getSendTo().trim())) {
			addActionError("收件人不能为空");
			return ERROR;
		}
		if (item.getContent() == null || "".equals(item.getContent().trim())) {
			addActionError("内容不能为空");
			return ERROR;
		}
		try {
			this.getMailItemService().executeSendMail(item, upload,
					uploadFileName,
					AppContext.getAccountContext().currentUser());
			this.setPrompt("邮件成功发送");
			return SUCCESS;
		} catch (InValidException e) {
			this.addActionError("邮件发送异常:" + e.getMessage());
			return ERROR;
		}

	}

	@Action("listMailOutBox")
	public String listMailOutBox() {
		this.setPaginiation(this.getMailItemService().listOutBox(
				this.getPaginationHelper().createQuery(MailItem.class),
				this.getPaginationHelper().prepare(paginiation),
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("viewSendMail")
	public String viewSendMail() {
		return SUCCESS;
	}

	@Action(value = "removeSendMail", results = { @Result(name = "success", type = "redirect", location = "listMailOutBox") })
	public String removeSendMail() {
		this.getMailItemService().batchRemove(itemIds);
		return SUCCESS;
	}
}
